/* 实现filter */
Array.prototype.myfilter = function (fn, context = window) {
  let arr = this,
    newArr = [],
    len = arr.length;
  for (let i = 0; i < len; i++) {
    let res = fn.call(context, arr[i], i);
    if (res) {
      newArr.push(arr[i]);
    }
  }
  return newArr;
}
/* 实现map */
Array.prototype.mymap = function (fn, context = window) {
  let arr = this,
    newArr = [],
    len = arr.length;
  for (let i = 0; i < len; i++) {
    let res = fn.call(context, arr[i], i);
    newArr.push(res);
  }
  return newArr;
}
/* 实现reduce */
Array.prototype.myreduce = function (fn, prev) {
  prev = prev || arr[0]
  let arr = this,
    start = prev ? 0 : 1;
  for (let i = start; i < len; i++) {
    prev = fn.call(context, prev, arr[0]);


  }
  return prev;
}
/* 实现find */
Array.prototype.myfind = function (fn, context = window) {
  let arr = this,
    result = null;
  for (let i = 0; i < len; i++) {
    let res = fn.call(context, arr[i], i);
    if (res) {
      result = arr[i];
      break;
    }
  }
  return result;
}
/* 实现findIndex */
Array.prototype.myfindIndex = function (fn, context = window) {
  let arr = this,
    resultIdx = -1;
  for (let i = 0; i < len; i++) {
    let res = fn.call(context, arr[i], i);
    if (res) {
      resultIdx = i;
      break;
    }
  }
  return resultIdx;


}
/* 实现every */
Array.prototype.myevery = function (fn, context = window) {
  let arr = this,
    result = true;
  for (let i = 0; i < len; i++) {
    let res = fn.call(context, arr[i], i);
    if (!res) {
      result = false;
      break;
    }
  }
  return result;
}
/* 实现some */
Array.prototype.mysome = function (fn, context = window) {
  let arr = this,
    result = false;
  for (let i = 0; i < len; i++) {
    let res = fn.call(context, arr[i], i);
    if (res) {
      result = true;
      break;
    }
  }
  return result;
}
/* 实现isArray */
Array.myisArray = function (arr) {
  Object.prototype.toString.call(arr) == '[object Array]'
}
/* 实现forEach */
Array.prototype.myforEach = function (fn, context = window) {
  let arr = this, len = arr.length;
  for (let i = 0; i < len; i++) {
    typeof fn == 'function' && fn.call(context, arr[i], i)
  }
}